package com.ruicar.afs.cloud.workflow.greatwall.controller.gtw;

import com.alibaba.fastjson.JSON;
import com.ruicar.afs.cloud.common.core.holder.TransRequestContextHolder;
import com.ruicar.afs.cloud.workflow.greatwall.component.GwtProcessComponent;
import com.ruicar.afs.cloud.workflow.greatwall.dto.GwtRequest;
import com.ruicar.afs.cloud.workflow.greatwall.exception.FlowProcessException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;


/**
 * The type GreatWallFlowController
 * <p>Description: </p>
 *
 * @author Fzero
 * @version 1.0
 * @date Created on 2020.07.09 09:38:17
 * @since 1.0
 */
@RestController
@AllArgsConstructor
@Slf4j
public class GreatWallFlowController {
    /**
     * Gwt work flow apis string
     * 流程引擎回调api地址
     *
     * @param request request
     * @return the string
     */
    @PostMapping("/flow/apis")
    public String gwtWorkFlowApis(@RequestBody String request) {
        TransRequestContextHolder.bindSysClientInfo(null);
        log.info("接收到工作流请求======>{}", request);
        GwtRequest gwtRequest = JSON.parseObject(request, GwtRequest.class);
        return GwtProcessComponent.gwtFlowMsgProcessor(gwtRequest.getMsgType()).process(gwtRequest);
    }


    @ExceptionHandler(FlowProcessException.class)
    @ResponseBody
    public String handleException(FlowProcessException ex, HttpServletResponse response) {
        response.setStatus(ex.getHttpStatus().value());
        return ex.getRtnMessage();
    }
}
